const http = require("http");
const config = require("../config");

const getarticle = async () => {
  //获取上线楼层新闻
  const result = new Promise((resolve, reject) => {
    let opt = {
      hostname: config.hostname,
      port: config.port,
      path: `/yesTop/ArticleList?status=1&p=1&s=200`,
      method: "GET"
    };
    let req = http.request(opt, res => {
      res.setEncoding("utf8");
      let resdata = "";
      res.on("data", chunk => {
        resdata += chunk;
      });
      res.on("end", () => {
        let responesedata = JSON.parse(resdata);
        if (responesedata.code == 200) {
          if (responesedata.data.list.length) {
            let createdobj = Object.create(null),
              resultarr = [];
            responesedata.data.list.forEach((ele, num) => {
              if (!createdobj[ele.code]) {
                createdobj[ele.code] = {
                  code: ele.code,
                  flooryesaDescribe: ele.list[0].yesnDescribe,
                  data: []
                };
              }
              createdobj[ele.code].data.push(ele);
            });
            for (let key in createdobj) {
              resultarr.push(createdobj[key]);
            }
            resolve(resultarr);
          } else {
            resolve([]);
          }
        } else {
          resolve([]);
        }
      });
    });
    req.on("error", e => {
      reject(e);
    });
    req.end();
  });
  return result;
};
const getNavigationList = async () => {
  //获取上线楼层导航栏
  const result = new Promise((resolve, reject) => {
    let opt = {
      hostname: config.hostname,
      port: config.port,
      path: `/yesTop/NavigationList?status=1&p=1&s=100`,
      method: "GET"
    };
    let req = http.request(opt, res => {
      res.setEncoding("utf8");
      let resdata = "";
      res.on("data", chunk => {
        resdata += chunk;
      });
      res.on("end", () => {
        let responsedata = JSON.parse(resdata);
        if (responsedata.code && responsedata.code == 200) {
          resolve(responsedata.data.list);
        } else {
          resolve([]);
        }
      });
    });
    req.on("error", e => {
      reject(e);
    });
    req.end();
  });
  return result;
};
const ownChoice = async (ctx, next) => {
  // const userid = ctx.session.userId
  const result = new Promise((resolve, reject) => {
    let opt = {
      hostname: config.hostname,
      port: config.port,
      path: `/yesSelection/select?userId=66691`,
      method: "GET"
    };
    let req = http.request(opt, res => {
      res.setEncoding("utf8");
      let resdata = "";
      res.on("data", chunk => {
        resdata += chunk;
      });
      res.on("end", () => {
        let responsedata = JSON.parse(resdata);
        if (responsedata.code && responsedata.code == 200) {
          resolve(responsedata.data.articles.list);
        } else {
          resolve([]);
        }
      });
    });
    req.on("error", e => {
      reject(e);
    });
    req.end();
  });
  return result;
};
module.exports = {
  getarticle,
  getNavigationList,
  ownChoice,
};
